<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm cmd-btn" id="jd_list" title="列出脚本">列出脚本</button>
        <button class="layui-btn layui-btn-sm" id="git_pull" title="更新脚本文件">更新脚本文件</button>
        <button class="layui-btn layui-btn-sm cmd-btn" id="rm_log" title="删除旧日志">删除旧日志</button>
        <button class="layui-btn layui-btn-sm cmd-btn" id="jd_get_share_code" title="获取互助码">获取互助码</button>
        <button class="layui-btn layui-btn-sm" id="export_sharecodes" title="导出互助码">导出互助码</button>
        <button class="layui-btn layui-btn-sm" id="ps" title="查看进程">查看进程</button>
        <button class="layui-btn layui-btn-sm" id="jd_cfd_loop" title="查看挂机日志">查看挂机日志</button>
        <button class="layui-btn layui-btn-sm" id="hangup" title="重启挂机程序">重启挂机程序</button>
        <button class="layui-btn layui-btn-sm" id="resetpwd" title="重置用户名密码">重置用户名密码</button>
      </div>

      <div>
        <form class="run-cmd-form" action="runCmd" method="POST">
            <div class="layui-form-item" style="display: flex;">
                <label class="layui-form-label">bash jd(.sh) </label>
                <input id="jdScript" style="width: 150px;" type="text" name="jdScript" lay-verify="title" autocomplete="off" placeholder="请输入脚本名称" class="layui-input">
                <button id="runCmd" type="submit" class="layui-btn" lay-submit="" lay-filter="demo1" style="margin-left: 10px;">立即执行</button>
              </div>
        </form>
      </div>
      <div class="container diffs wrapper" style="height: 70vh;">
        <textarea id="code" name="code"></textarea>
      </div>
    </div>
  </div>
  
  <script type="text/javascript">
    $.ajaxSetup({ cache: false });

    $(document).ready(function () {
        var timer = 0;
        var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
            lineNumbers: true,
            lineWrapping: false,
            styleActiveLine: true,
            matchBrackets: true,
            readOnly: true,
            mode: 'text',
            theme: 'dracula'
        });

        $('.cmd-btn').click(function() {
            let confirmTxt, cmd;
            switch (this.id) {
                case 'rm_log':
                    confirmTxt = '确认删除旧日志？';
                    cmd = 'bash rm_log.sh 2>&1 | tee log/rm_log.log';
                    break;
                case 'jd_list':
                    // confirmTxt = '';
                    cmd = 'bash jd.sh';
                    break;
                case 'jd_get_share_code':
                    confirmTxt = '确认获取互助码？';
                    cmd = `bash jd.sh ${this.id} now`;
                    break;
                default:
                    break;
            }



            // if (confirmTxt && !confirm(confirmTxt)) {
            //     return;
            // }

            if (timer) {
                layer.msg('请等待上一条任务执行完毕。');
                return;
            }

            if (confirmTxt) {
                layer.confirm(confirmTxt, function (index) {
                    layer.close(index);
                    runCmd()
                });
            } else {
                runCmd();
            }

            function runCmd() {
                editor.setValue('');

                const jsName = this.id === 'jd_list' ? '' : this.id;
                $.post('./runCmd', { cmd }, function (data) {
                    editor.setValue(data.msg);

                    if (jsName) {
                        //将光标和滚动条设置到文本区最下方
                        editor.execCommand('goDocEnd');
                    }

                    clearInterval(timer);
                    timer = 0;
                });

                timer = 1;
                // 1s后开始查日志
                setTimeout(() => {
                    jsName && getLogInterval(jsName);
                }, 1000);
            }
        });

        $('#hangup, #resetpwd, #export_sharecodes, #git_pull, #ps, #jd_cfd_loop').click(function() {
            let confirmTxt, cmd;
            switch (this.id) {
                case 'git_pull':
                    confirmTxt = '确认更新脚本文件？';
                    cmd = 'bash git_pull.sh 2>&1 | tee -a log/git_pull.log';
                    break;
                case 'export_sharecodes':
                    confirmTxt = '确认导出互助码？';
                    cmd = `bash ${this.id}.sh`;
                    break;
                case 'ps':
                    // confirmTxt = '确认查看进程？';
                    cmd = `${this.id}`;
                    break;
                case 'jd_cfd_loop':
                    // confirmTxt = '确认查看挂机日志？';
                    cmd = `cat /root/.pm2/logs/jd-cfd-loop-out.log 2>&1`;
                    break;
                case 'hangup':
                    confirmTxt = '确认重启挂机程序？';
                    cmd = `bash jd.sh ${this.id}`;
                    break;
                case 'resetpwd':
                    confirmTxt = '重置用户名密码？';
                    cmd = `bash jd.sh ${this.id}`;
                    break;
                default:
                    break;
            }

            if (timer) {
                layer.msg('请等待上一条任务执行完毕。');
                return;
            }

            if (confirmTxt) {
                layer.confirm(confirmTxt, function (index) {
                    layer.close(index);
                    runCmd()
                });
            } else {
                runCmd();
            }

            function runCmd() {
                editor.setValue('');
                $.post('./runCmd', { cmd }, function (data) {
                    editor.setValue(data.msg);

                    clearInterval(timer);
                    timer = 0;
                });
                timer = 1;
            }
        });


        $('#runCmd').click(function() {
            const jdScript = $('#jdScript').val();
            if (!jdScript) {
                layer.msg('请先补充命令。');
                return;
            }

            if (timer) {
                layer.msg('请等待上一条任务执行完毕。');
                return;
            }
            editor.setValue('');

            const cmd = `bash jd.sh ${jdScript} now`;
            $.post('./runCmd', {cmd: cmd, delay: 1000}, function (data) {
                editor.setValue(data.msg);

                //将光标和滚动条设置到文本区最下方
                editor.execCommand('goDocEnd');

                clearInterval(timer);
                timer = 0;
            });

            timer = 1;
            // 1s后开始查日志
            setTimeout(() => {
                getLogInterval(jdScript);
            }, 1000);

            return false;
        });


        function getLogInterval(jsName) {
            timer && clearInterval(timer);

            // 先执行一次
            getLog(jsName);
            timer = setInterval(() => {
                getLog(jsName);
            }, 1500);
        }

        function getLog(jsName) {
            $.get(`./runLog/${jsName}`, function (data) {
                if (data !== 'no logs') {
                    editor.setValue(data);
                }
                //将光标和滚动条设置到文本区最下方
                editor.execCommand('goDocEnd');
            });
        }

        $('#wrap').click(function () {
            var lineWrapping = editor.getOption('lineWrapping');
            editor.setOption('lineWrapping', !lineWrapping);
        });

        $('#jd_list').trigger('click');
    });
    layui.use(["layer"], function () {
      var $ = layui.jquery,
        layer = layui.layer;
    });
  </script>
  